package cn.xiaoyaoji.plugin.html;

import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class DocConvertor
{
  private boolean ready;
  private List<GroupConvertor> groupConvertors = new ArrayList();
  
  public boolean convert(List<List<String>> xmList, List<String> groupnames)
  {
    this.ready = false;
    int groupIndex = 0;
   for (int i = 0; i < xmList.size(); i++) 
   {
	   GroupConvertor convertor = new GroupConvertor(groupIndex);
        if (convertor.convert(xmList.get(i)))
        {
          convertor.setGroupName(groupnames.get(i));
          this.groupConvertors.add(convertor);
          groupIndex++;
          this.ready = true;
        }
    }
    return this.ready;
  }
  
  public String toHtmlString()
  {
    if (!this.ready) {
      return "";
    }
    String templateString = HtmlTemplates.getInstance().getHtmlTemplate();
    templateString = templateString.replace("${dir_groups}", getDirGroupsString());
    templateString = templateString.replace("${groups}", getGroupsString());
    templateString = templateString.replace("${last_group}", String.valueOf(this.groupConvertors.size() + 1));
    return templateString;
  }
  
  private String getGroupsString()
  {
    if (!this.ready) {
      return "";
    }
    StringBuilder sBuilder = new StringBuilder();
    for (GroupConvertor convertor : this.groupConvertors) {
      sBuilder.append(convertor.toHtmlString());
    }
    return sBuilder.toString();
  }
  
  private String getDirGroupsString()
  {
    if (!this.ready) {
      return "";
    }
    StringBuilder sBuilder = new StringBuilder();
    for (GroupConvertor convertor : this.groupConvertors) {
      sBuilder.append(convertor.toDirHtmlString());
    }
    return sBuilder.toString();
  }
  
  private File[] listFolders(File rootFolder)
  {
    File[] folders = rootFolder.listFiles(new FileFilter()
    {
      public boolean accept(File file)
      {
        return (file.isDirectory()) && (!".".equals(file.getName())) && (!"..".equals(file.getName()));
      }
    });
    return folders;
  }

  /*public static void main(String[] args)
  {
    if (args.length < 4)
    {
      System.out.println("argument error!");
      System.out.println("usage# java -jar DocConvert.jar srcFolder dstFile.html");
      return;
    }
    File rootFolder = new File(args[0]);
    if (!rootFolder.isDirectory())
    {
      System.out.println(args[0] + " is not a folder");
      return;
    }
    try
    {
      String destFile = args[1];
      XmlConvertor.argsDsfMethodName = args[2];
      XmlConvertor.argsIsNeedDB = args[3];
      
      DocConvertor convertor = new DocConvertor();
      boolean res = convertor.convert(rootFolder);
      if (!res)
      {
        System.out.println("convert root folder " + args[0] + " failed!");
        return;
      }
      String htmlString = convertor.toHtmlString();
      FileOutputStream fos = new FileOutputStream(destFile);
      fos.write(htmlString.getBytes("UTF-8"));
      fos.flush();
      fos.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }*/
}
